﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RibbonEditor.Rules;
using System.Reflection;
using System.Xml;

namespace RibbonEditor.Forms.Rules
{
    public partial class RuleSelectionDialog : Form
    {
        public enum RuleType
        {
            EnableRule,
            DisplayRule,
            TabDisplayRule,
            OrGroupFromEnable,
            OrGroupFromDisplay
        }

        private RuleType currentType;

        public object SelectedRule { get; set; }

        public RuleSelectionDialog(RuleType type)
        {
            InitializeComponent();
            this.currentType = type;
            this.FillRulesList(type);
        }

        private void FillRulesList(RuleType type)
        {
            string[] ruleList = null;

            switch (type)
            {
                case RuleType.EnableRule:
                    {
                        ruleList = new string[]{"AuthenticationRule","CrmClientTypeRule","CrmOfflineAccessStateRule",
                                                "CrmOutlookClientTypeRule","CustomRule","EntityRule","FormStateRule",
                                                "OrRule","OutlookItemTrackingRule","OutlookVersionRule","PageRule",
                                                "RecordPrivilegeRule","SelectionCountRule","SkuRule","ValueRule"};
                    }
                    break;
                case RuleType.DisplayRule:
                    {
                       ruleList = new string[]{"CrmClientTypeRule",
                                                "CrmOfflineAccessStateRule",
                                                "EntityPrivilegeRule",
                                                "EntityPropertyRule",
                                                "EntityRule",
                                                "FormEntityContextRule",
                                                "FormStateRule",
                                                "MiscellaneousPrivilegeRule",
                                                "OrganizationSettingRule",
                                                "OrRule",
                                                "OutlookRenderTypeRule",
                                                "OutlookVersionRule",
                                                "PageRule",
                                                "ReferencingAttributeRequiredRule",
                                                "RelationshipTypeRule",
                                                "SkuRule",
                                                "ValueRule"};
                    }
                    break;
                case RuleType.TabDisplayRule:
                    {
                        ruleList = new string[] { "EntityRule", "PageRule" };
                    }
                    break;
                case RuleType.OrGroupFromEnable:
                    {
                        ruleList = new string[]{"AuthenticationRule",
                                                "CrmClientTypeRule",
                                                "CrmOutlookClientTypeRule",
                                                "CrmOfflineAccessStateRule",
                                                "CustomRule",
                                                "EntityRule",
                                                "FormStateRule",
                                                "OutlookItemTracking",
                                                "OutlookVersionRule",
                                                "PageRule",
                                                "RecordPrivilegeRule",
                                                "SelectionCountRule",
                                                "SkuRule",
                                                "ValueRule"};
                    }
                    break;
                case RuleType.OrGroupFromDisplay:
                    {
                        ruleList = new string[]{"AuthenticationRule",
                                                "CrmClientTypeRule",
                                                "CrmOutlookClientTypeRule",
                                                "CrmOfflineAccessStateRule",
                                                "EntityRule",
                                                "EntityPrivilegeRule",
                                                "EntityPropertyRule",
                                                "FormEntityContextRule",
                                                "FormStateRule",
                                                "MiscellaneousPrivilegeRule",
                                                "OrganizationSettingRule",
                                                "OutlookRenderTypeRule",
                                                "OutlookVersionRule",
                                                "PageRule",
                                                "ReferencingAttributeRequiredRule",
                                                "RelationshipTypeRule",
                                                "SkuRule",
                                                "ValueRule"};
                    }
                    break;
                  
            }

            this.cboRulesList.Items.Clear();

            foreach (string rule in ruleList)
            {
                this.cboRulesList.Items.Add(rule);
            }
        }

        private void btnRuleSelectionValidate_Click(object sender, EventArgs e)
        {
            if (this.cboRulesList.SelectedItem == null)
            {
                MessageBox.Show(this, "Please select a rule in the list", "Information", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                Assembly assembly = Assembly.GetExecutingAssembly();
                AssemblyName assemblyName = assembly.GetName();

                Type t = Type.GetType(assemblyName.Name + ".Rules." + this.cboRulesList.SelectedItem.ToString());

                switch (this.currentType)
                {
                    case RuleType.EnableRule:
                    case RuleType.OrGroupFromEnable:
                        IEnableRule rule = (IEnableRule)Activator.CreateInstance(t);
                        this.SelectedRule = rule;
                        break;
                    case RuleType.DisplayRule:
                    case RuleType.OrGroupFromDisplay:
                        IDisplayRule irule = (IDisplayRule)Activator.CreateInstance(t);
                        this.SelectedRule = irule;
                        break;
                    case RuleType.TabDisplayRule:
                        ITabDisplayRule trule = (ITabDisplayRule)Activator.CreateInstance(t);
                        this.SelectedRule = trule;
                        break;
                }

                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        private void btnRuleSelectionCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
